package com.ruix.app.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ruix.app.model.Comment;
import com.ruix.app.util.LogUitl;
import com.ruix.app.widget.comment.CommentClick;
import com.ruix.app.widget.comment.SpannableStringBuilderCompat;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hadexs on 2017/5/22.
 */

public class PostContentView extends TextView{


    //用户名颜色
    private int textColor = 0xff333333;
    private int authorColor = 0xff15b86c;
    private int eventColor = 0xffc6c6c6;
    private SpannableStringBuilderCompat spannableStringBuilderCompat;
    private String url;


    public PostContentView(Context context) {
        super(context);
        setMovementMethod(MyLinkMovementMethod.getInstance());
    }

    public PostContentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setMovementMethod(MyLinkMovementMethod.getInstance());
    }

    public PostContentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setMovementMethod(MyLinkMovementMethod.getInstance());
    }

    public void setContentText(String contentText) {
        boolean containtag =  contentText.contains("#");
//        boolean containUrl =  contentText.contains(" ");
        setSpannableString(contentText,containtag,true);
//        if (containtag){
//
//        } else {
//            setText(contentText);
//        }

    }


    private void setSpannableString(String contentText,boolean containTag, boolean containUrl){
        SpannableString ss = new SpannableString(contentText);
        if (containTag){
            String regEx = "#([^#]+?)#";//="#[^#]+#";  // 编译正则表达式
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(contentText);
            ArrayList<String> tags = new ArrayList();
            ArrayList<Integer> start = new ArrayList();
            ArrayList<Integer> end = new ArrayList();
            // 字符串是否与正则表达式相匹配
            while (matcher.find()){
                tags.add(matcher.group());
                start.add(matcher.start());
                end.add(matcher.end());
            }
            for (int i = 0;i<start.size();i++){
                ss.setSpan(new TagClickableSpan(tags.get(i)), start.get(i),end.get(i) ,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }

        if (containUrl){
            String regex = "[a-zA-z]+://[^\\s]*";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(contentText);
            ArrayList<String> urls = new ArrayList();
            ArrayList<Integer> start = new ArrayList();
            ArrayList<Integer> end = new ArrayList();
            // 字符串是否与正则表达式相匹配
            while (matcher.find()){
                urls.add(matcher.group());
                start.add(matcher.start());
                end.add(matcher.end());
            }
            for (int i = 0;i<urls.size();i++){
                ss.setSpan(new MyClickableSpan(urls.get(i)), start.get(i), end.get(i),
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
        setText(ss);
    }


}
